package sing.core.ext.view

import android.graphics.Color
import android.graphics.Typeface
import android.text.*
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import android.widget.TextView
import sing.core.callback.Callback


fun TextView.toExpand(str: String,maxLine:Int = 2,color: Int = Color.parseColor("#FFFFFF"),expand:String = "展开",listener:View.OnClickListener?=null) {
    val text: CharSequence = text

    val width: Int = width
    val paint: TextPaint = paint
    val layout: Layout = layout
    val line: Int = layout.lineCount
    if (line > maxLine) {
        val start: Int = layout.getLineStart(maxLine-1)
        val end: Int = layout.getLineVisibleEnd(maxLine-1)
        val lastLine = text.subSequence(start, end)
        val expandWidth = paint.measureText(expand)
        val remain = width - expandWidth
        val ellipsize: CharSequence = TextUtils.ellipsize(lastLine, paint, remain, TextUtils.TruncateAt.END)
        val clickableSpan: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                if(listener == null){
                    toCollapse(str,maxLine,expand = expand)
                }else{
                    listener.onClick(widget)
                }
            }
            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = color
                ds.isUnderlineText = false
                ds.isFakeBoldText = true //加粗设置
            }
        }
        val ssb = SpannableStringBuilder()
        ssb.append(text.subSequence(0, start))
        ssb.append(ellipsize)
        ssb.append(expand)
        ssb.setSpan(ForegroundColorSpan(color), ssb.length - expand.length, ssb.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        ssb.setSpan(clickableSpan, ssb.length - expand.length, ssb.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        movementMethod = LinkMovementMethod.getInstance()
        this.text = ssb
    }
}


fun TextView.toCollapse(str: String,maxLine:Int = 2,color: Int = Color.parseColor("#FFFFFF"),expand:String = "展开",listener:View.OnClickListener?=null) {
    val collapse = "收起"
    // 默认此时文本肯定超过行数了，直接在最后拼接文本
    val clickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            if(listener == null){
                toExpand(str,maxLine,expand = expand)
            }else{
                listener.onClick(widget)
            }
        }
        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            ds.color = color
            ds.isUnderlineText = false
        }
    }

    val ssb = SpannableStringBuilder()
    ssb.append(str)
    ssb.append(collapse)
    ssb.setSpan(ForegroundColorSpan(color), ssb.length - collapse.length, ssb.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
    ssb.setSpan(clickableSpan, ssb.length - collapse.length, ssb.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

    movementMethod = LinkMovementMethod.getInstance()
    text = ssb
}


fun TextView.toExpand(str: CharSequence,maxLine:Int = 2,color: Int = Color.parseColor("#FFFFFF"),expand:String = "展开",callback: Callback) {
    val text: CharSequence = text

    val width: Int = width
    val paint: TextPaint = paint
    val layout: Layout = layout
    val line: Int = layout.lineCount
    if (line > maxLine) {
        val start: Int = layout.getLineStart(maxLine-1)
        val end: Int = layout.getLineVisibleEnd(maxLine-1)
        val lastLine = text.subSequence(start, end)
        val expandWidth = paint.measureText(expand)
        val remain = width - expandWidth
        val ellipsize: CharSequence = TextUtils.ellipsize(lastLine, paint, remain, TextUtils.TruncateAt.END)
        val clickableSpan: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                callback.result()
            }
            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = color
                ds.isUnderlineText = false
            }
        }
        val ssb = SpannableStringBuilder()
        ssb.append(text.subSequence(0, start))
        ssb.append(ellipsize)
        ssb.append(expand)
        ssb.setSpan(ForegroundColorSpan(color), ssb.length - expand.length, ssb.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        ssb.setSpan(clickableSpan, ssb.length - expand.length, ssb.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        ssb.setSpan(StyleSpan(Typeface.BOLD), ssb.length - expand.length, ssb.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE) // 加粗
        movementMethod = LinkMovementMethod.getInstance()
        this.text = ssb
    }
}